//
//  HomeScreenItemCell.m
//  TopGirls
//
//  Created by Khang on 10/1/13.
//
//

#import "HomeScreenItemCell.h"
#import "HomeScreenItem.h"

@implementation HomeScreenItemCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        subCells = [NSMutableArray array];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)dealloc {
    if (subCells) {
        for (UIView* sub in subCells) {
            [sub removeFromSuperview];
        }
        [subCells removeAllObjects];
    }
    subCells = nil;
}

#pragma mark - reused
- (void)prepareForReuse {
    [super prepareForReuse];
    
    for (UIView* sub in subCells) {
        sub.hidden = YES;
    }
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    HomeScreenItem* item = (HomeScreenItem*)self.cellObject;
    if (item) {
        for (NSInteger index = 0; index < item.numberSubCell; index ++) {
            UIView* subView = [subCells objectAtIndex:index];
            subView.frame = [[item.rectSubCells objectAtIndex:index] CGRectValue];
            subView.hidden = NO;
        }
    }
}

#pragma mark - set object
- (void)setObject:(id)object {
    [super setObject:object];
    
    HomeScreenItem* item = (HomeScreenItem*)self.cellObject;
    if (object) {
        for (NSInteger index = 0; index < item.numberSubCell; index ++) {
            if (index >= [subCells count]) {
                
                UIView* subView = [self createSubItemCell:[item.icons objectAtIndex:index]
                                                    title:@"Khang"
                                                  atIndex:index];
                [subCells addObject:subView];
                
            } else {
                UIView* subView = [subCells objectAtIndex:index];
                [self updateSubItemCell:subView withValue:[item.icons objectAtIndex:index]
                                  title:@"Khang" atIndex:index];
            }
        }
    }
}

#pragma mark - height
+ (CGFloat)heightForCellWithObject:(id)object ofTableView:(UITableView *)tableview {
    HomeScreenItem* item = (HomeScreenItem*)object;
    
    CGFloat maxW = tableview.frame.size.width;
    maxW = (maxW / item.numberSubCell);
    CGFloat maxH = 110.f;
    
    CGFloat x = 0;
    CGFloat y = 0;
    for (NSInteger index = 0; index < item.numberSubCell; index ++) {
        [item.rectSubCells addObject:[NSValue valueWithCGRect:CGRectMake(x, y, maxW, maxH)]];
        
        x += maxW;
    }
    
    return maxH;
}

#pragma mark -
#pragma mark Private
- (UIView*)createSubItemCell:(id)iconUrl title:(NSString*)tle atIndex:(NSInteger)idx {
    UIView* subView = [[UIView alloc] initWithFrame:CGRectZero];
    subView.backgroundColor = COLORCLEAR;
    
    UIButton* subButton = [UIButton buttonWithType:UIButtonTypeCustom];
    subButton.frame = CGRectMake(0, 0, subView.frame.size.width, subView.frame.size.height);
    subButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    subButton.backgroundColor = COLORCLEAR;
    subButton.backgroundColor = iconUrl; //Just for test
    [subButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    subButton.tag = idx + 1;
    [subView addSubview:subButton];
    
    UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, subView.frame.size.width, 40)];
    titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    titleLabel.backgroundColor = COLORA(110, 110, 110, 0.5f);
    titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    titleLabel.numberOfLines = 1;
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.text = tle;
    titleLabel.tag = (idx + 1)*10;
    [subView addSubview:titleLabel];
    
    [self.contentView addSubview:subView];
    
    return subView;
}
- (void)updateSubItemCell:(UIView*)subView withValue:(id)iconUrl title:(NSString*)tle atIndex:(NSInteger)idx {
    UIButton* button = (UIButton*)[subView viewWithTag:idx + 1];
    if (button && [button isKindOfClass:[UIButton class]]) {
        button.backgroundColor = iconUrl; //Just for test
    }
    
    UILabel* label = (UILabel*)[subView viewWithTag:(idx + 1)*10];
    if (label && [label isKindOfClass:[UILabel class]]) {
        label.text = tle;
    }
}

- (void)buttonPress:(id)sender {
    NSLog(@"+++ Pressed %d", [sender tag]);
}

@end
